package com.whmnx.my.ui.viewmodel;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.databinding.ObservableList;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.whmnx.base.bean.BaseBean;
import com.whmnx.base.bean.OrderDetailBean;
import com.whmnx.base.bean.OrderListBean;
import com.whmnx.base.bean.RequestBean;
import com.whmnx.base.constants.Constants;
import com.whmnx.base.http.CallBack;
import com.whmnx.base.http.HttpClient;
import com.whmnx.base.utils.UItils;
import com.whmnx.base.view.CommonDialog2;
import com.whmnx.base.view.CommonDialog3;
import com.whmnx.my.BR;
import com.whmnx.my.R;
import com.whmnx.my.ui.activity.AppraiseActivity;
import com.whmnx.my.ui.activity.AppraiseXiangActivity;
import com.whmnx.my.ui.activity.RequestActivity;
import com.whmnx.my.ui.fragment.MyOrderDetailsFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.functions.Consumer;
import me.goldze.mvvmhabit.base.ItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * author : huang
 * date   : 2020/9/15 0015 16:48
 * version: 1.0
 * @author Administrator
 */
public class OrderItemViewModel extends ItemViewModel {

    private  String order_id1;
    public  String order_id;
    public ObservableField<String> Order_StateDesc = new ObservableField<>("");
    public ObservableField<String> Order_OrderNo = new ObservableField<>("");
    public ObservableField<String> Order_State = new ObservableField<>("");

    public ObservableField<String> Order_pay = new ObservableField<>("");
    public ObservableField<String> Address_Name = new ObservableField<>("");
    public ObservableField<String> Address_Mobile = new ObservableField<>("");
    public ObservableField<String> Address_Label = new ObservableField<>("");
    public ObservableField<String> Product_Price = new ObservableField<>("");
    public ObservableField<String> CreateDate = new ObservableField<>("");




    public ObservableInt viewVisibility = new ObservableInt(View.GONE);
    public ObservableInt view2Visibility = new ObservableInt(View.VISIBLE);
    private FinancialModel viewModel;
    private ViewPagerCaiwuItemViewModel viewPagerItemViewModel;
    private OrderListBean orderListBean;

    public OrderItemViewModel(@NonNull FinancialModel viewModel,ViewPagerCaiwuItemViewModel viewPagerItemViewModel, OrderListBean productListBean) {
        super(viewModel);
        this.viewModel = viewModel;
        this.viewPagerItemViewModel = viewPagerItemViewModel;
        this.orderListBean = productListBean;
        order_id = productListBean.getOrder_OrderNo();
        order_id1 = productListBean.getOrder_Id();

        Order_OrderNo.set("订单编号: "+ productListBean.getOrder_OrderNo());
        Address_Name.set(productListBean.getAddress().getAddress_Name());
        Address_Mobile.set(productListBean.getAddress().getAddress_Mobile());
        CreateDate.set("下单时间: "+ productListBean.getCreateDate());

      /*  if (TextUtils.isEmpty(addressBean.getAddress_LabelDetail())){
            Address_Label.setText("收货地址: " +  addressBean.getAddress_Detailed());
        }else {
            tvAddress.setText("收货地址: " +  addressBean.getAddress_Detailed() + " " + addressBean.getAddress_LabelDetail());
        }*/
        Address_Label.set(productListBean.getAddress().getAddress_Label() + productListBean.getAddress().getAddress_Detailed());

        //Order_StateDesc
        Order_StateDesc.set(productListBean.getOrder_StateDesc());
        //最右边的按钮
        //  Order_pay.set(UItils.getOrderState(productListBean.getOrder_State()));
        Order_State.set(UItils.getOrderState(productListBean.getOrder_State()));
        //0待支付
        if (productListBean.getOrder_State().equals("0")){
            viewVisibility.set(View.VISIBLE);
            Order_State.set("取消订单");
        }else if (productListBean.getOrder_State().equals("1")){
            viewVisibility.set(View.GONE);
            Order_State.set("确认发货");
        }else if (productListBean.getOrder_State().equals("2")){
            viewVisibility.set(View.GONE);
            Order_State.set("查看详情");
           // Order_pay.set("确认收货");
        }else if (productListBean.getOrder_State().equals("3")){
            viewVisibility.set(View.VISIBLE);
            view2Visibility.set(View.INVISIBLE);
            Order_pay.set("去回复");
        }else if (productListBean.getOrder_State().equals("11")){
            viewVisibility.set(View.VISIBLE);
            view2Visibility.set(View.VISIBLE);
            Order_State.set("同意");
            Order_pay.set("拒绝");
        }else if (productListBean.getOrder_State().equals("7") || productListBean.getOrder_State().equals("9") || productListBean.getOrder_State().equals("15")){  // 15 已完成
            viewVisibility.set(View.INVISIBLE);
            view2Visibility.set(View.INVISIBLE);
        }else if (productListBean.getOrder_State().equals("4")){  //用户已回复
            viewVisibility.set(View.VISIBLE);
            view2Visibility.set(View.INVISIBLE);
            Order_pay.set("去回复");
        }else if (productListBean.getOrder_State().equals("5") || productListBean.getOrder_State().equals("12")){  //5用户已回复  12 同意退货
            viewVisibility.set(View.INVISIBLE);
            view2Visibility.set(View.INVISIBLE);
        }

        Order_OrderNo.set("订单编号：" +  productListBean.getOrder_OrderNo());

        for (OrderListBean.ProductListBean productListBeans : productListBean.getProductList()) {
            observableList.add(new OrderItemViewModel3(viewModel,productListBeans,order_id1));
        }
    }


    //给RecyclerView添加ObservableList
    public ObservableList<OrderItemViewModel3> observableList = new ObservableArrayList<>();
    //给RecyclerView添加ItemBinding
    public ItemBinding<OrderItemViewModel3> itemBinding = ItemBinding.of(BR.viewModel, R.layout.grid_order);


    //取消订单
    public BindingCommand OnClickCommand = new BindingCommand(() -> {
        if (Order_State.get().equals("取消订单")){
            String s2 = "是否取消订单";
            new CommonDialog2(viewModel.mFragment.getActivity(), R.style.dialog, s2,"", (dialog, confirm) -> {
                if (confirm) {
                    updorderStatus(-1);
                }
                dialog.dismiss();
            }).show();
        }else if (Order_State.get().equals("申请退款")){

            String s2 = "是否申请退款";
            new CommonDialog2(viewModel.mFragment.getActivity(), R.style.dialog, s2,"", (dialog, confirm) -> {
                if (confirm) {
                        Map<String, Object> params = new HashMap<>();
                    List<String> list = new ArrayList<>();
                    params.put("order_OrderNo",order_id);
                    params.put("backRemark","");
                    params.put("order_State",5);
                    params.put("imageModel",list);
                    params.put("orderItem_BackRemark","");
                    params.put("logisticsName","");
                    params.put("courierNo","");

                    HttpClient.post(viewModel.mFragment.getActivity(), Constants.BACKORDER, params,new CallBack<OrderDetailBean>() {
                        @Override
                        public void onSuccess(OrderDetailBean result) {
                            viewPagerItemViewModel.getSelorder(true);
                        }
                    });



                  //  updorderStatus(5);
                }
                dialog.dismiss();
            }).show();


        }else if (Order_State.get().equals("申请退货")){
            Bundle bundle = new Bundle();
            //订单号
            bundle.putString("OrderId",order_id1);
            bundle.putString("Order_OrderNo",order_id);
            viewModel.startActivity(RequestActivity.class,bundle);
        }else if (Order_State.get().equals("确认发货")){
            String s2 = "是否确认发货";
            new CommonDialog2(viewModel.mFragment.getActivity(), R.style.dialog, s2,"", (dialog, confirm) -> {
                if (confirm) {
                    //待收货
                    Constants.ORDER = "确认发货";
                    updorderStatus(2);
                }
                dialog.dismiss();
            }).show();
        }else if (Order_State.get().equals("查看详情")){
            Bundle bundle = new Bundle();
            //订单号
            bundle.putString("Order_Id",order_id1);
            viewModel.startContainerActivity(MyOrderDetailsFragment.class.getCanonicalName(),bundle);
        }else if (Order_State.get().equals("同意")){   ////状态 7
            String s2 = "是否同意退款";
            new CommonDialog2(viewModel.mFragment.getActivity(), R.style.dialog, s2,"", (dialog, confirm) -> {
                if (confirm) {
                    RequestBean requestBean  =  new RequestBean();
                    requestBean.setOrder_OrderNo(order_id);
                  //  requestBean.setBackRemark(etInput.get());
                    requestBean.setOrder_State(7);
                    Map<String, Object> params = new HashMap<>();
                    List<String> list = new ArrayList<>();
                    params.put("order_OrderNo",order_id);
                    params.put("backRemark","");
                    params.put("order_State",7);
                    params.put("imageModel",list);
                    params.put("orderItem_BackRemark","");
                    params.put("logisticsName","");
                    params.put("courierNo","");

                    HttpClient.post(viewModel.mFragment.getActivity(), Constants.BACKORDER, params,new CallBack<OrderDetailBean>() {
                        @Override
                        public void onSuccess(OrderDetailBean result) {
                            viewPagerItemViewModel.getSelorder(true);
                        }
                    });
                }
                dialog.dismiss();
            }).show();
        }
    });



    //立即支付
    public BindingCommand OnClickCommand2 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (Order_pay.get().equals("去回复")){
                Bundle bundle = new Bundle();
                //订单号
                bundle.putString("Order_Id",order_id1);
                viewModel.startContainerActivity(MyOrderDetailsFragment.class.getCanonicalName(),bundle);
            }else if (Order_pay.get().equals("确认收货")){  //状态 7
                //待评价
                String s2 = "是否确认收货";
                new CommonDialog2(viewModel.mFragment.getActivity(), R.style.dialog, s2,"", (dialog, confirm) -> {
                    if (confirm) {
                        updorderStatus(3);
                    }
                    dialog.dismiss();
                }).show();
            }else if (Order_pay.get().equals("去评价")){
                if (orderListBean.getProductList().size() > 1){
                    Bundle bundle = new Bundle();
                    bundle.putString("OrderId",order_id);
                    viewModel.startActivity(AppraiseXiangActivity.class,bundle);
                }else {
                    Bundle bundle = new Bundle();
                    bundle.putString("OrderId",order_id);
                    viewModel.startActivity(AppraiseActivity.class,bundle);
                }
            }else if (Order_pay.get().equals("拒绝")){  // 状态 9
                String s2 = "申请理由";
                new CommonDialog3(viewModel.mFragment.getActivity(), R.style.dialog, s2,1, (dialog, confirm, input, s) -> {
                    if (confirm) {
                        RequestBean requestBean  =  new RequestBean();
                        requestBean.setOrder_OrderNo(order_id1);
                        requestBean.setBackRemark(s2);
                        requestBean.setOrder_State(9);

                        Map<String, Object> params = new HashMap<>();
                        List<String> list = new ArrayList<>();
                        params.put("order_OrderNo",order_id);
                        params.put("backRemark",s2);
                        params.put("order_State",9);
                        params.put("imageModel",list);
                        params.put("orderItem_BackRemark","");
                        params.put("logisticsName","");
                        params.put("courierNo","");

                        HttpClient.post(viewModel.mFragment.getActivity(), Constants.BACKORDER, params,new CallBack<OrderDetailBean>() {
                            @Override
                            public void onSuccess(OrderDetailBean result) {
                                viewPagerItemViewModel.getSelorder(true);
                            }
                        });
                    }
                    dialog.dismiss();
                }).show();

            }
        }
    });

    //打电话
    public BindingCommand OnClickCommand3 = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            String s2 = "tel:" + orderListBean.getAddress().getAddress_Mobile();
            new CommonDialog2(viewModel.mFragment.getActivity(), R.style.dialog, s2,"", (dialog, confirm) -> {
                if (confirm) {
                    //请求打开相机权限
                    RxPermissions rxPermissions = new RxPermissions(viewModel.mFragment);
                    rxPermissions.request(Manifest.permission.CALL_PHONE)
                            .subscribe(new Consumer<Boolean>() {
                                @Override
                                public void accept(Boolean aBoolean) throws Exception {
                                    if (aBoolean) {
                                        Intent intent = new Intent(Intent.ACTION_DIAL);
                                        Uri data = Uri.parse("tel:" + orderListBean.getAddress().getAddress_Mobile());
                                        intent.setData(data);
                                        viewModel.mFragment.startActivity(intent);
                                    } else {

                                    }
                                }
                            });
                }
                dialog.dismiss();
            }).show();
        }
    });



    private void updorderStatus(int type) {
        Map<String, Object> params = new HashMap<>();
        params.put("orderNo",order_id);
        params.put("orderStatus",type + "");

        HttpClient.get(viewModel.mFragment.getActivity(), Constants.UPDORDERSTATUS, params,new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean result) {
                viewPagerItemViewModel.getSelorder(true);
               // viewPagerItemViewModel.autoRefresh2(2);

            }
        });
    }
}
